package com.taxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taxx.entity.DataUploadRecord;
import com.taxx.entity.OperationLog;
import com.taxx.entity.TaxApiLog;
import com.taxx.mapper.DataUploadRecordMapper;
import com.taxx.mapper.OperationLogMapper;
import com.taxx.mapper.TaxApiLogMapper;
import com.taxx.service.SystemLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 系统日志服务实现类
 * 
 * @author taxx
 * @since 2024-01-01
 */
@Slf4j
@Service
public class SystemLogServiceImpl implements SystemLogService {

    @Autowired
    private DataUploadRecordMapper dataUploadRecordMapper;

    @Autowired
    private TaxApiLogMapper taxApiLogMapper;

    @Autowired
    private OperationLogMapper operationLogMapper;

    @Override
    public IPage<DataUploadRecord> getDataImportLogs(int page, int size, String fileName, String dataType, Integer status) {
        Page<DataUploadRecord> pageParam = new Page<>(page, size);
        
        QueryWrapper<DataUploadRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        
        if (StringUtils.hasText(fileName)) {
            queryWrapper.like("file_name", fileName);
        }
        if (StringUtils.hasText(dataType)) {
            queryWrapper.eq("data_type", dataType);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        
        return dataUploadRecordMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public IPage<TaxApiLog> getApiLogs(int page, int size, String apiName, Integer status) {
        Page<TaxApiLog> pageParam = new Page<>(page, size);
        
        QueryWrapper<TaxApiLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        
        if (StringUtils.hasText(apiName)) {
            queryWrapper.like("api_name", apiName);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        
        return taxApiLogMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public IPage<OperationLog> getOperationLogs(int page, int size, String username, String operation, Integer status) {
        Page<OperationLog> pageParam = new Page<>(page, size);
        
        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        
        if (StringUtils.hasText(username)) {
            queryWrapper.like("username", username);
        }
        if (StringUtils.hasText(operation)) {
            queryWrapper.like("operation", operation);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        
        return operationLogMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public boolean deleteLog(Long logId, String logType) {
        try {
            switch (logType) {
                case "upload":
                    return dataUploadRecordMapper.deleteById(logId) > 0;
                case "api":
                    return taxApiLogMapper.deleteById(logId) > 0;
                case "operation":
                    return operationLogMapper.deleteById(logId) > 0;
                default:
                    log.warn("未知的日志类型: {}", logType);
                    return false;
            }
        } catch (Exception e) {
            log.error("删除日志失败, logId: {}, logType: {}", logId, logType, e);
            return false;
        }
    }

    @Override
    public boolean clearLogs(String logType) {
        try {
            switch (logType) {
                case "upload":
                    return dataUploadRecordMapper.delete(null) > 0;
                case "api":
                    return taxApiLogMapper.delete(null) > 0;
                case "operation":
                    return operationLogMapper.delete(null) > 0;
                default:
                    log.warn("未知的日志类型: {}", logType);
                    return false;
            }
        } catch (Exception e) {
            log.error("清空日志失败, logType: {}", logType, e);
            return false;
        }
    }

    @Override
    public void recordDataImportLog(DataUploadRecord info) {
        try {
            dataUploadRecordMapper.insert(info);
        } catch (Exception e) {
            log.error("记录数据导入日志失败", e);
        }
    }

    @Override
    public void recordOperationLog(OperationLog info) {
        try {
            operationLogMapper.insert(info);
        } catch (Exception e) {
            log.error("记录操作日志失败", e);
        }
    }
}
